<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Family extends CI_Controller {

	
	 public function __construct() {
		parent::__construct();
		$this->load->library('session');
		$this->load->model('family_model');
		$this->load->helper('url');
	}
	public function index()
	{
		$this->load->view('templates/fmly_head');		
		$this->load->view('family/login');
		$this->load->view('templates/foot');
	}
	
	public function login() 
	{
		//接收POST信息，并与数据库中用户密码对照，如果正确信息将存入Session。
		$id = $this->input->post('id');
		$passwd = $this->input->post('passwd');
		
		$user = $this->family_model->get_by_id ($id);
		
		if (password_verify($passwd, $user['passwd'])) {
		
		$newdata = array (
			'user_id' => $user['id']		
		)	;
		
		$this->session->set_userdata($newdata);
		
		$data['user_id'] = $user['id'];
		//加载模版解析类
		$this->load->library('parser');
		$this->load->view('templates/fmly_head');
		$this->parser->parse('family/success', $data);
		$this->load->view('family/menu');
		$this->load->view('templates/fmly_foot');
		} else {
			echo "<a href='index'>登录失败请重新登录。</a>";
		}
		
	}
	
	
	//新增
	public function create() {
		
		if (isset($_POST['id'])) {
			
			$this->load->view('templates/fmly_head');
			$data = $this->family_model->create();
			$this->load->view('templates/foot');
		}
		else {
			$this->load->view('templates/fmly_head');
			$this->load->view('family/form_create');
			$this->load->view('templates/fmly_foot');
		}
		
	}
	
	
	//已登录后的主页
	public function menu() {
		isset($_SESSION['user_id']) OR exit('请先登录！');
		
		$this->load->view('templates/fmly_head');
		$this->load->view('family/menu');
		$this->load->view('templates/fmly_foot');
		
		
	}
	
}